package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.dao.SetmealMapper;
import com.itheima.dto.SetmealDto;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;

import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /*
     *新增套餐接口*/
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //添加套餐基本数据到Setmeal表中

        this.save(setmealDto);
        //添加套餐菜品数据到SetmealDish表中
        Long setmealId = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);


    }

    /*
     * 删除套餐*/
    public void deleteWithDish(List<Long> ids) {


        //删除套餐
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        wrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(wrapper);
        if (count > 0) {
            //抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //可以删除
        this.removeByIds(ids);
        //删除套餐菜品
        LambdaQueryWrapper<SetmealDish> setmealDishwrapper = new LambdaQueryWrapper<>();
        setmealDishwrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(setmealDishwrapper);


    }

    /*
     * 根据套餐id查询套餐数据*/
    public SetmealDto getByIdWithDish(Long id) {
        //查询套餐基本数据
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        //copy对象
        BeanUtils.copyProperties(setmeal, setmealDto);

        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;


    }

    /*
     * 修改套餐*/
    public void updateWithDish(SetmealDto setmealDto) {
        //修改套餐基本数据
        this.updateById(setmealDto);

        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        //删除套餐里面的菜品
        setmealDishService.remove(wrapper);
        //添加菜品到套餐
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item -> {
            //将setmealDto的id赋值给item
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //添加到etmealDish表中
        setmealDishService.saveBatch(setmealDishes);

    }


}
